// pages/register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTime: 60,
    disabled: false,
    time: '获取验证码',
    username:'',
    phone:'',
    password:'',
    repassword:''
  },
  login(){
    wx.navigateBack({
      delta: 1
    })
  },
  retreat() {
    wx.navigateBack({
      delta: 1
    })
  },
  //发送验证码
  sendMsg() {
    console.log(11111);
    var that = this;
    var currentTime = that.data.currentTime;
    // that.setData({
    //   time: (currentTime-1) + '秒'
    // })
    let interval = setInterval(function () {
      that.setData({
        time: (currentTime - 1) + '秒',
        disabled: true
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新获取',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)

  },
  //---得到输入框的内容函数
  uservalue(e){
   
    let username = e.detail.value.replace(/\s+/g, '');
    console.log(username)
    this.setData({
      username
    })
  },
  phonevalue(e){
    let phone = e.detail.value.replace(/\s+/g, '');
    this.setData({
      phone
    })
  },
  passvalue(e){
    let password = e.detail.value.replace(/\s+/g, '');
    this.setData({
      password
    })
  },
  repassvalue(e){
    let repassword = e.detail.value.replace(/\s+/g, '');
    this.setData({
      repassword
    })
  },
  // 网络请求函数
  registerAjax(){
    let account = this.data.username;
    let phone = this.data.phone;
    let password = this.data.password;
    let repass =this.data.repassword;
    if(account.length==0){
      wx.showToast({
        title: '账号不能为空',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    
    if(phone.length<11){
      wx.showToast({
        title: '手机号不能为空或手机号错误',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (password.length==''){
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    console.log(password.length)
   
    if(password.length<6){
      wx.showToast({
        title: '请输入6-12位密码',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (password.length > 12) {
      wx.showToast({
        title: '请输入6-12位密码',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if(password!=repass){
      wx.showToast({
        title: '2次密码不一致',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    let sendData={
      account: this.data.username,
      phone:this.data.phone,
      password: this.data.password,
    }
    let baseURL = "http://192.168.0.106:8090/xzhotel/";
     new Promise((resolve, reject) => {
      wx.request({
        url: baseURL + 'user/save',
        method: 'post',
        data: sendData,
        success: resolve,
        fail: reject,
      })
    }).then(res => {
      if (res.data.code === 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000,
          mask: true,
          success: function () {
            setTimeout(function () {
              wx.navigateBack({
                data: 1
              })
            }, 2000)
          }
        })
      }
    }).catch(err => {

    })
  },
  registerClick(){
    this.registerAjax()
  },
  //点击注册按钮
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.setNavigationBarTitle({
        title: '用户注册',
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})